package com.ifacebox.face.track.sevice;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author znn
 */
public class FaceThreadService extends ThreadPoolExecutor {

    public FaceThreadService(int threads) {
        super(threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new FaceThreadFactory());
    }

    @Deprecated
    public FaceThreadService() {
        super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), new FaceThreadFactory());
    }

    public static class FaceThreadFactory implements ThreadFactory {
        private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
        private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1);
        private final String name;

        public FaceThreadFactory() {
            this.name = String.format("face-track-pool-%d-thread-", POOL_NUMBER.getAndIncrement());
        }

        @Override
        public Thread newThread(Runnable run) {
            return new Thread(run, this.name + THREAD_NUMBER.incrementAndGet()) {
                {
                    this.setDaemon(true);
                }
            };
        }
    }
}
